package GuiTests;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class dateValidator extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected JTextField yearField , monthField , dayField;
	protected JTextArea yearBox , monthBox , dayBox;
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				startGUI();
			}
		});

	}

	private static void startGUI() {
		JFrame frame = new JFrame("Date Validator");
		//JLabel instructions = new JLabel("Please enter a date to validate.");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.add(new dateValidator());
		frame.setSize(500, 200);
	}

	public dateValidator() {
		super(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel instructions = new JLabel("Please enter a date to validate between 1000 AD and 2000 AD.");
		
		yearField = new JTextField(20);
		JLabel yearlabel = new JLabel("Year:");
		monthField = new JTextField(20);
		JLabel monthlabel = new JLabel("Month:");
		dayField = new JTextField(20);
		JLabel daylabel = new JLabel("Day:");
		
		JButton val = new JButton("Validate");
		val.addActionListener(this);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		add(instructions , c);
		c.gridy = 1;
		c.gridx = 0;
		add(yearlabel, c);
		c.gridx = 1;
		add(yearField , c);
		c.gridy = 3;
		c.gridx = 0;
		add(monthlabel , c);
		c.gridx = 1;
		add(monthField , c);
		c.gridy = 4;
		c.gridx = 0;
		add (daylabel , c);
		c.gridx = 1;
		add(dayField , c);
		c.gridy = 5;
		add(val , c);
	}
	
	private static boolean valiDate(int year, int month, int day) {
		//validate inputs
		//boolean dateValid;
		boolean monthValid = ( 0 < month && month < 13);
	    if (!monthValid)
	    {
	    	month = 1;
	    }
	    //Check to see if year is valid
	    int[] monthDays = {
				31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	    boolean yearValid = ( 999 < year && year < 2000);
	    boolean leapYear = true;
		//Determine whether it's a leap year
	    if (year % 4 == 0)
	    {
	    	if (year % 100 == 0)
	    	{
	    		leapYear = (year % 400 == 0);
	    	}
	    }
	    else
	    	{
	    		leapYear = false;
	    	}
	    
	    
	    //System.out.println(leapYear);
		int daysInMonth;
		//Determine number of days in month
	    if (leapYear && (month == 2))
	    {
	    	daysInMonth = 29;
	    }
	    else
	    {
	    	daysInMonth = monthDays[(month - 1)];
	    }
		//User number of days in month to check to see if day is valid
	    boolean dayValid = ( 0 < day && day <= daysInMonth );
		//Determine whether date is valid and print appropriate message
	    return (yearValid && monthValid && dayValid);
	}
	

	@Override
	public void actionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		//try {
			int year = Integer.parseInt(yearField.getText());	
		//}
		//catch(NumberFormatException nfe) {
			
		//}
		int month = Integer.parseInt(monthField.getText());
		int day = Integer.parseInt(dayField.getText());
		boolean dateValid;
		dateValid = valiDate(year, month, day);
		
		JFrame resultBox = new JFrame("Result:");
		JLabel answer = new JLabel();
		//JButton okay = new JButton("Okay.");
		if(dateValid) {
			answer.setText("The date is valid");
		}
		else {
			answer.setText("The date is invalid");
		}
		
		JPanel pane = new JPanel();
		resultBox.setVisible(true);
		resultBox.setSize(200, 100);
		resultBox.add(pane);
		pane.add(answer);
		//pane.add(okay);
		
	}
		
		
}


